package com.ruoyi.bearing.service.Impl;

import java.nio.file.StandardCopyOption;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bearing.mapper.ComponentdataMapper;
import com.ruoyi.bearing.domain.Componentdata;
import com.ruoyi.bearing.service.IComponentdataService;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import org.springframework.web.multipart.MultipartFile;
/**
 * 数据入库Service业务层处理
 *
 * @author lx
 * @date 2025-03-19
 */
@Service
public class ComponentdataServiceImpl implements IComponentdataService
{
    @Autowired
    private ComponentdataMapper componentdataMapper;

    @Autowired
    private RuoYiConfig ruoyiConfig;

    /**
     * 查询数据入库
     *
     * @param id 数据入库主键
     * @return 数据入库
     */
    @Override
    public Componentdata selectComponentdataById(Long id)
    {
        return componentdataMapper.selectComponentdataById(id);
    }

    /**
     * 查询数据入库列表
     *
     * @param componentdata 数据入库
     * @return 数据入库
     */
    @Override
    public List<Componentdata> selectComponentdataList(Componentdata componentdata)
    {
        return componentdataMapper.selectComponentdataList(componentdata);
    }

    /**
     * 新增数据入库
     *
     * @param componentdata 数据入库
     * @return 结果
     */
    @Override
    public int insertComponentdata(Componentdata componentdata)
    {
        return componentdataMapper.insertComponentdata(componentdata);
    }

    /**
     * 修改数据入库
     *
     * @param componentdata 数据入库
     * @return 结果
     */
    @Override
    public int updateComponentdata(Componentdata componentdata)
    {
        return componentdataMapper.updateComponentdata(componentdata);
    }

    /**
     * 批量删除数据入库
     *
     * @param ids 需要删除的数据入库主键
     * @return 结果
     */
    @Override
    public int deleteComponentdataByIds(Long[] ids)
    {
        return componentdataMapper.deleteComponentdataByIds(ids);
    }

    /**
     * 删除数据入库信息
     *
     * @param id 数据入库主键
     * @return 结果
     */
    @Override
    public int deleteComponentdataById(Long id)
    {
        return componentdataMapper.deleteComponentdataById(id);
    }

    /**
     * 上传文件
     *
     * @param file 文件
     * @param componentdata 数据入库
     * @return 结果
     */
    @Override
    public int uploadFile(MultipartFile file, Componentdata componentdata) {
        try {
            // 定义文件存储路径
            String uploadDir = ruoyiConfig.getUpload();
            File directory = new File(uploadDir);
            if (!directory.exists()) {
                directory.mkdirs();
            }

            // 生成文件名
            String fileName = file.getOriginalFilename();
            Path filePath = Paths.get(uploadDir + "/" + fileName);
            // 检查文件是否存在，如果存在则删除
            if (Files.exists(filePath)) {
                Files.delete(filePath);
            }
            try {
                // 使用StandardCopyOption.REPLACE_EXISTING替代先删除再复制
                Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                throw new IOException("无法保存文件: " + filePath, e);
            }

            // 设置文件路径和创建时间
            componentdata.setDataFilePath(filePath.toString());
            componentdata.setCreatedTime(new Date());
            // 多加一个判断，数据集名称不能重复
            if (componentdata.getId() == 0L && componentdataMapper.selectCountByName(componentdata.getName(), null) > 0){

                throw new RuntimeException("数据集名称不能重复");
            } else if (componentdata.getId() != 0L) {
                if (componentdataMapper.selectCountByName(componentdata.getName(), componentdata.getId()) > 0){
                    throw new RuntimeException("数据集名称不能重复");
                }
            }

            if (componentdata.getId() == 0L){
                return componentdataMapper.insertComponentdata(componentdata);
            }
            return componentdataMapper.updateComponentdata(componentdata);
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 保存特性文件路径
     *
     * @param componentdata 数据入库
     * @return 结果
     */
    @Override
    public int saveCharacteristicFilePath(Componentdata componentdata) {
        try {
            // 更新数据库记录
            return componentdataMapper.updateComponentdata(componentdata);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Componentdata> selectAll() {
        return componentdataMapper.selectAll();
    }
}
